feat: add unified request context interfaces for MCP operations#70
Merged
tzolov merged 4 commits intospring-ai-community:mainfrom Oct 6, 2025
Merged
Conversation
Introduces McpSyncRequestContext and McpAsyncRequestContext as unified interfaces for accessing MCP request context across both stateful and stateless operations. These new context types provide: - Unified API that works for both stateful and stateless operations - Convenient methods for logging, progress updates, sampling, and elicitation - Type-safe access to request data and context - Automatic injection by the framework Key changes: - Added McpSyncRequestContext and McpAsyncRequestContext interfaces - Added default implementations (DefaultMcpSyncRequestContext, DefaultMcpAsyncRequestContext) - Updated all tool method callbacks to support new context types - Updated JSON schema generator to exclude context parameters - Added tests for new context functionality - Updated README with detailed documentation and examples - Deprecated @McpProgressToken in favor of internal handling by context - Marked McpSyncServerExchange and McpAsyncServerExchange as deprecated in favor of McpSyncRequestContext and McpAsyncRequestContext Resolves spring-ai-community#69 Releated to spring-projects/spring-ai#4471 Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Replace Consumer<ElicitationSpec> pattern with direct TypeReference parameters for better type safety and simpler API. Add StructuredElicitResult<T> for type-safe responses and automatic Map-to-POJO conversion using Jackson. Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
- Rename `elicitation()` methods to `elicit()` across all context interfaces - Rename `sampling()` methods to `sample()` across all context interfaces - Standardize elicit() to always return `StructuredElicitResult<T>` instead of raw type - Replace positional parameters with builder-style `Consumer<ElicitationSpec>` configuration - Add support for both `Class<T>` and `TypeReference<T>` type parameters - Introduce `ElicitationSpec` interface with fluent API for message and metadata - Update README documentation with new method signatures and examples - Update all test cases to use new API methods - Add copyright header to StructuredElicitResult class Breaking Changes: - `elicitation(TypeReference<T>)` now returns `Optional<StructuredElicitResult<T>>` instead of `Optional<T>` - `elicitation(TypeReference<T>, String, Map)` replaced with `elicit(Consumer<ElicitationSpec>, TypeReference<T>)` - `sampling(...)` renamed to `sample(...)` - All elicit methods now consistently return structured results with action, content, and metadata Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
lglauer
reviewed
Oct 18, 2025
| boolean hasOtherParams = Arrays.stream(method.getParameters()).anyMatch(param -> { | ||
| Class<?> type = param.getType(); | ||
| return !CallToolRequest.class.isAssignableFrom(type) | ||
| return !McpSyncRequestContext.class.isAssignableFrom(type) |
There was a problem hiding this comment.
IMO McpTransportContext should be excluded here too. Currently parameters of type McpTransportContext are included in the generated json schema. Stateless servers may inject McpTransportContext parameters according to docs: https://docs.spring.io/spring-ai/reference/1.1/api/mcp/mcp-annotations-examples.html#_stateless_server_examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces McpSyncRequestContext and McpAsyncRequestContext as unified interfaces for accessing MCP request context across both stateful and stateless operations. These new context types provide:
Key changes:
Resolves #69
Releated to spring-projects/spring-ai#4471